package de.fmaul.common.io;

import java.io.File;

import com.google.common.base.Predicate;

public class FileExtensionMatchPredicate implements Predicate<File> {

	private final String extension;
	private final boolean caseSensitive;

	public FileExtensionMatchPredicate(String extension, boolean caseSensitive) {
		this.extension = extension;
		this.caseSensitive = caseSensitive;
	}

	public boolean apply(File input) {
		if (caseSensitive) {
			return input.getName().endsWith(extension);
		} else {
			return endsWithIgnoreCase(input.getName(), extension);
		}
	}

	private boolean endsWithIgnoreCase(final String target, final String suffix) {
		return target.regionMatches(true, target.length() - suffix.length(),
				suffix, 0, suffix.length());
	}

}
